package com.jeesite.modules.wzt.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.banboocloud.Codec.BamboocloudFacade;
import com.jeesite.common.config.Global;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.wzt.entity.AttributeEntity;
import com.jeesite.modules.wzt.service.WztAsgService;
import com.jeesite.modules.wzt.service.WztOrgService;
import com.jeesite.modules.wzt.service.WztUserService;
import com.jeesite.modules.wzt.util.BamboocloudUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "wzt/api")
public class WztApiController extends BaseController {
    //SM4
    private static final String key = "JLB6L6MJVIT5Z7IL";
    private static final String type = "SM4";
    private static final String signType = "SM3";
    @Autowired
    private WztUserService wztUserService;
    @Autowired
    private WztOrgService wztOrgService;
    @Autowired
    private WztAsgService wztAsgService;
    /**
     *  用户属性对照
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    @PostMapping(value = "SchemaService")
    public void schemaService(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String bodyparam = BamboocloudUtils.getRequestBody(req);
        System.out.println("first--param-->" + bodyparam);
        //报文解密
        bodyparam = BamboocloudUtils.getPlaintext(bodyparam, key, type);
        PrintWriter out = resp.getWriter();
        Map<String,Object> schema = new HashMap<>();
        try {
            System.out.println("json--param-->" + bodyparam);
            Map<String,Object> reqmap = (Map<String,Object>) JSON.parse(bodyparam);
            String username = (String) reqmap.get("bimRemoteUser");
            String password = (String) reqmap.get("bimRemotePwd");
            boolean  isTrue= BamboocloudUtils.checkUsernamePassword(username, password);
            if(!isTrue){
                schema.put("resultCode", 500);
                schema.put("message", "账号或者密码错误");
                responseResult(out, schema);
                return;
            }

            JSONObject jsonObject = JSONObject.parseObject( Global.getConfig("platform.wzt.SchemaService"));
            List<AttributeEntity> orgs = jsonObject.getObject("organization", List.class);
            System.out.println(orgs.toString());
            List<AttributeEntity> accs = jsonObject.getObject("account", List.class);
            System.out.println(accs.toString());
            schema.put("account", accs);
            schema.put("organization", orgs);
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response---json-->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, key, type);
            out.write(mapJson);
            out.close();

        } catch (Exception e) {
            schema.put("resultCode", 500);
            schema.put("message", "解析返回失败");
            responseResult(out, schema);
            e.printStackTrace();
        }
    }

    /**
     *  账号创建
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    @PostMapping(value = "UserCreateService")
    public void userCreateService(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String bodyparam = BamboocloudUtils.getRequestBody(req);
        System.out.println("first--param-->" + bodyparam);
        //报文解密
        bodyparam = BamboocloudUtils.getPlaintext(bodyparam, key, type);
        PrintWriter out = resp.getWriter();
        Map<String,Object> schema = new HashMap<>();
        try {
            System.out.println("json--param-->" + bodyparam);
            Map<String,Object> reqmap = (Map<String,Object>) JSON.parse(bodyparam);
            String username = (String) reqmap.get("bimRemoteUser");
            String password = (String) reqmap.get("bimRemotePwd");
            boolean  isTrue= BamboocloudUtils.checkUsernamePassword(username, password);
            if(!isTrue){
                schema.put("resultCode", 500);
                schema.put("message", "授权账号或者密码错误");
                responseResult(out, schema);
                return;
            }
            schema = wztUserService.createUser(reqmap);
           if(StringUtils.equals("500",(String)schema.get("resultCode"))){
               responseResult(out, schema);
               return;
           }
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "success");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response---json-->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, key, type);
            out.write(mapJson);
            out.close();

        } catch (Exception e) {
            schema.put("resultCode", 500);
            schema.put("message", "创建账号失败:"+ e.getMessage());
            responseResult(out, schema);
            e.printStackTrace();
        }
    }


    /**
     *  账号更新
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    @PostMapping(value = "UserUpdateService")
    public void  userUpdateService(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String bodyparam = BamboocloudUtils.getRequestBody(req);
        System.out.println("first--param-->" + bodyparam);
        //报文解密
        bodyparam = BamboocloudUtils.getPlaintext(bodyparam, key, type);
        PrintWriter out = resp.getWriter();
        Map<String,Object> schema = new HashMap<>();
        try {
            System.out.println("json--param-->" + bodyparam);
            Map<String,Object> reqmap = (Map<String,Object>) JSON.parse(bodyparam);
            String username = (String) reqmap.get("bimRemoteUser");
            String password = (String) reqmap.get("bimRemotePwd");
            boolean  isTrue= BamboocloudUtils.checkUsernamePassword(username, password);
            if(!isTrue){
                schema.put("resultCode", 500);
                schema.put("message", "授权账号或者密码错误");
                responseResult(out, schema);
                return;
            }
            schema = wztUserService.updateuser(reqmap);
            if(StringUtils.equals("500",(String)schema.get("resultCode"))){
                responseResult(out, schema);
                return;
            }
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "success");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response---json-->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, key, type);
            out.write(mapJson);
            out.close();

        } catch (Exception e) {
            schema.put("resultCode", 500);
            schema.put("message", "账号更新失败:"+ e.getMessage());
            responseResult(out, schema);
            e.printStackTrace();
        }
    }


    /**
     *  账号删除
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    @PostMapping(value = "UserDeleteService")
    public void  userDeleteService(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String bodyparam = BamboocloudUtils.getRequestBody(req);
        System.out.println("first--param-->" + bodyparam);
        //报文解密
        bodyparam = BamboocloudUtils.getPlaintext(bodyparam, key, type);
        PrintWriter out = resp.getWriter();
        Map<String,Object> schema = new HashMap<>();
        try {
            System.out.println("json--param-->" + bodyparam);
            Map<String,Object> reqmap = (Map<String,Object>) JSON.parse(bodyparam);
            String username = (String) reqmap.get("bimRemoteUser");
            String password = (String) reqmap.get("bimRemotePwd");
            boolean  isTrue= BamboocloudUtils.checkUsernamePassword(username, password);
            if(!isTrue){
                schema.put("resultCode", 500);
                schema.put("message", "授权账号或者密码错误");
                responseResult(out, schema);
                return;
            }
            schema = wztUserService.deleteUser(reqmap);
            if(StringUtils.equals("500",(String)schema.get("resultCode"))){
                responseResult(out, schema);
                return;
            }
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "success");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response---json-->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, key, type);
            out.write(mapJson);
            out.close();

        } catch (Exception e) {
            schema.put("resultCode", 500);
            schema.put("message", "账号删除失败:"+ e.getMessage());
            responseResult(out, schema);
            e.printStackTrace();
        }
    }



    private void responseResult(PrintWriter out, Map<String, Object> schema) {
        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, key, type);
        out.write(mapJson);
        out.close();
    }


    /**
     *  组织创建
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    @PostMapping(value = "OrgCreateService")
    public void  orgCreateService(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String bodyparam = BamboocloudUtils.getRequestBody(req);
        System.out.println("first--param-->" + bodyparam);
        //报文解密
        bodyparam = BamboocloudUtils.getPlaintext(bodyparam, key, type);
        PrintWriter out = resp.getWriter();
        Map<String,Object> schema = new HashMap<>();
        try {
            System.out.println("json--param-->" + bodyparam);
            Map<String,Object> reqmap = (Map<String,Object>) JSON.parse(bodyparam);
            String username = (String) reqmap.get("bimRemoteUser");
            String password = (String) reqmap.get("bimRemotePwd");
            boolean  isTrue= BamboocloudUtils.checkUsernamePassword(username, password);
            if(!isTrue){
                schema.put("resultCode", 500);
                schema.put("message", "授权账号或者密码错误");
                responseResult(out, schema);
                return;
            }
            schema = wztOrgService.createOrg(reqmap);
            if(StringUtils.equals("500",(String)schema.get("resultCode"))){
                responseResult(out, schema);
                return;
            }
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "success");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response---json-->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, key, type);
            out.write(mapJson);
            out.close();

        } catch (Exception e) {
            schema.put("resultCode", 500);
            schema.put("message", "组织创建失败:"+ e.getMessage());
            responseResult(out, schema);
            e.printStackTrace();
        }
    }


    /**
     *  组织更新
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    @PostMapping(value = "OrgUpdateService")
    public void  orgUpdateService(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String bodyparam = BamboocloudUtils.getRequestBody(req);
        System.out.println("first--param-->" + bodyparam);
        //报文解密
        bodyparam = BamboocloudUtils.getPlaintext(bodyparam, key, type);
        PrintWriter out = resp.getWriter();
        Map<String,Object> schema = new HashMap<>();
        try {
            System.out.println("json--param-->" + bodyparam);
            Map<String,Object> reqmap = (Map<String,Object>) JSON.parse(bodyparam);
            String username = (String) reqmap.get("bimRemoteUser");
            String password = (String) reqmap.get("bimRemotePwd");
            boolean  isTrue= BamboocloudUtils.checkUsernamePassword(username, password);
            if(!isTrue){
                schema.put("resultCode", 500);
                schema.put("message", "授权账号或者密码错误");
                responseResult(out, schema);
                return;
            }
            schema = wztOrgService.updateOrg(reqmap);
            if(StringUtils.equals("500",(String)schema.get("resultCode"))){
                responseResult(out, schema);
                return;
            }
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "success");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response---json-->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, key, type);
            out.write(mapJson);
            out.close();

        } catch (Exception e) {
            schema.put("resultCode", 500);
            schema.put("message", "组织更新失败:"+ e.getMessage());
            responseResult(out, schema);
            e.printStackTrace();
        }
    }



    /**
     *  组织删除
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    @PostMapping(value = "OrgDeleteService")
    public void  orgDeleteService(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String bodyparam = BamboocloudUtils.getRequestBody(req);
        System.out.println("first--param-->" + bodyparam);
        //报文解密
        bodyparam = BamboocloudUtils.getPlaintext(bodyparam, key, type);
        PrintWriter out = resp.getWriter();
        Map<String,Object> schema = new HashMap<>();
        try {
            System.out.println("json--param-->" + bodyparam);
            Map<String,Object> reqmap = (Map<String,Object>) JSON.parse(bodyparam);
            String username = (String) reqmap.get("bimRemoteUser");
            String password = (String) reqmap.get("bimRemotePwd");
            boolean  isTrue= BamboocloudUtils.checkUsernamePassword(username, password);
            if(!isTrue){
                schema.put("resultCode", 500);
                schema.put("message", "授权账号或者密码错误");
                responseResult(out, schema);
                return;
            }
            schema = wztOrgService.deleteOrg(reqmap);
            if(StringUtils.equals("500",(String)schema.get("resultCode"))){
                responseResult(out, schema);
                return;
            }
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "success");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response---json-->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, key, type);
            out.write(mapJson);
            out.close();

        } catch (Exception e) {
            schema.put("resultCode", 500);
            schema.put("message", "组织更新失败:"+ e.getMessage());
            responseResult(out, schema);
            e.printStackTrace();
        }
    }

    /**
     *  创建任职
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    @PostMapping(value = "AsgCreateService")
    public void  asgCreateService(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String bodyparam = BamboocloudUtils.getRequestBody(req);
        System.out.println("first--param-->" + bodyparam);
        //报文解密
        bodyparam = BamboocloudUtils.getPlaintext(bodyparam, key, type);
        PrintWriter out = resp.getWriter();
        Map<String,Object> schema = new HashMap<>();
        try {
            System.out.println("json--param-->" + bodyparam);
            Map<String,Object> reqmap = (Map<String,Object>) JSON.parse(bodyparam);
            String username = (String) reqmap.get("bimRemoteUser");
            String password = (String) reqmap.get("bimRemotePwd");
            boolean  isTrue= BamboocloudUtils.checkUsernamePassword(username, password);
            if(!isTrue){
                schema.put("resultCode", 500);
                schema.put("message", "授权账号或者密码错误");
                responseResult(out, schema);
                return;
            }
            schema = wztAsgService.creatAsg(reqmap);
            if(StringUtils.equals("500",(String)schema.get("resultCode"))){
                responseResult(out, schema);
                return;
            }
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "success");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response---json-->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, key, type);
            out.write(mapJson);
            out.close();

        } catch (Exception e) {
            schema.put("resultCode", 500);
            schema.put("message", "创建任职失败:"+ e.getMessage());
            responseResult(out, schema);
            e.printStackTrace();
        }
    }

    /**
     *  更新任职
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    @PostMapping(value = "AsgUpdateService")
    public void  asgUpdateService(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String bodyparam = BamboocloudUtils.getRequestBody(req);
        System.out.println("first--param-->" + bodyparam);
        //报文解密
        bodyparam = BamboocloudUtils.getPlaintext(bodyparam, key, type);
        PrintWriter out = resp.getWriter();
        Map<String,Object> schema = new HashMap<>();
        try {
            System.out.println("json--param-->" + bodyparam);
            Map<String,Object> reqmap = (Map<String,Object>) JSON.parse(bodyparam);
            String username = (String) reqmap.get("bimRemoteUser");
            String password = (String) reqmap.get("bimRemotePwd");
            boolean  isTrue= BamboocloudUtils.checkUsernamePassword(username, password);
            if(!isTrue){
                schema.put("resultCode", 500);
                schema.put("message", "授权账号或者密码错误");
                responseResult(out, schema);
                return;
            }
            schema = wztAsgService.updateAsg(reqmap);
            if(StringUtils.equals("500",(String)schema.get("resultCode"))){
                responseResult(out, schema);
                return;
            }
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "success");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response---json-->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, key, type);
            out.write(mapJson);
            out.close();

        } catch (Exception e) {
            schema.put("resultCode", 500);
            schema.put("message", "更新任职失败:"+ e.getMessage());
            responseResult(out, schema);
            e.printStackTrace();
        }
    }

    /**
     *  查询任职
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    @PostMapping(value = "QueryAsgService")
    public void  queryAsgService(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String bodyparam = BamboocloudUtils.getRequestBody(req);
        System.out.println("first--param-->" + bodyparam);
        //报文解密
        bodyparam = BamboocloudUtils.getPlaintext(bodyparam, key, type);
        PrintWriter out = resp.getWriter();
        Map<String,Object> schema = new HashMap<>();
        try {
            System.out.println("json--param-->" + bodyparam);
            Map<String,Object> reqmap = (Map<String,Object>) JSON.parse(bodyparam);
            String username = (String) reqmap.get("bimRemoteUser");
            String password = (String) reqmap.get("bimRemotePwd");
            boolean  isTrue= BamboocloudUtils.checkUsernamePassword(username, password);
            if(!isTrue){
                schema.put("resultCode", 500);
                schema.put("message", "授权账号或者密码错误");
                responseResult(out, schema);
                return;
            }
            schema = wztAsgService.queryAsg(reqmap);
            if(StringUtils.equals("500",(String)schema.get("resultCode"))){
                responseResult(out, schema);
                return;
            }
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "success");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response---json-->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, key, type);
            out.write(mapJson);
            out.close();

        } catch (Exception e) {
            schema.put("resultCode", 500);
            schema.put("message", "查询任职失败:"+ e.getMessage());
            responseResult(out, schema);
            e.printStackTrace();
        }
    }

}
